package com.yb;

/**
 * 位运算和数学的关系
 */
public class BitOperation {
    public static void main(String[] args) {
        test2();
    }

    /**
     * 左（右）移n位相当于乘以（除以）2的n次方
     */
    public void test1() {
        int x = 100;
        int y = x << 2;
        x = x << 4;
        System.out.println(y);
        System.out.println(x);
        y <<= 1;
        System.out.println(y);
        x = x >> 4;
        System.out.println(x);
    }

    /**
     * 不使用中间变量交换x和y的值
     */
    public static void test2() {
        int x = 12;
        int y = 14;
        x = x ^ y;
        System.out.println(x);
        y = y ^ x;
        System.out.println(y);
        x = x ^ y;
        System.out.println(x);
    }
}
